package com.polaris.live.dialog

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.cache.CommonCache
import com.polaris.live.common.R.color
import com.polaris.live.databinding.DialogMyBeansWhatBinding
import com.polaris.live.resp.back_resp.CoinBeansResp
import com.polaris.live.translation.R.string

/**
 * MyBeansWhatDialog
 *
 * @author Created by 半仙 on 2023/10/25/025 11:35
 */
class MyBeansWhatDialog(
    context: Context,
    private val mCoinBeansResp: CoinBeansResp,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogMyBeansWhatBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_my_beans_what
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogMyBeansWhatBinding.bind(popupImplView)
        CommonCache.setUserIsShow()
        val incomeMessage = context.getString(string.withdraw_income_tip)
        val incomeBuilder = SpannableStringBuilder(incomeMessage)
        val income = context.getString(string.withdraw_income)
        val incomeIndex = incomeMessage.indexOf(income)
        if (incomeIndex != -1) {
            incomeBuilder.setSpan(
                clickableSpan,
                incomeIndex,
                incomeIndex + income.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        mBinding.tvIncome.apply {
            highlightColor = ColorUtils.getColor(R.color.color_767678)
            movementMethod = LinkMovementMethod.getInstance()
            text = incomeBuilder
        }

        val rewardMessage = context.getString(string.withdraw_reward_tip)
        val rewardBuilder = SpannableStringBuilder(rewardMessage)
        val reward = context.getString(string.withdraw_reward)
        val rewardIndex = rewardMessage.indexOf(reward)
        if (rewardIndex != -1) {
            rewardBuilder.setSpan(
                clickableSpan,
                rewardIndex,
                rewardIndex + reward.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        mBinding.tvReward.apply {
            highlightColor = ColorUtils.getColor(R.color.color_767678)
            movementMethod = LinkMovementMethod.getInstance()
            text = rewardBuilder
        }

        //顶部钱
        val gainCoin = mCoinBeansResp.gainCoin
        val sendGainCoin = mCoinBeansResp.sendGainCoin
        val spanIncomes = SpanUtils.with(mBinding.tvIncomes)
            .append(StringUtils.getString(string.income))
            .append(":  ")
            .setForegroundColor(ColorUtils.getColor(color.color_202028))
            .append("$gainCoin")
            .setForegroundColor(ColorUtils.getColor(color.color_FE4F3C))
            .create()
        mBinding.tvIncomes.text = spanIncomes
        val spanRewards = SpanUtils.with(mBinding.tvRewards)
            .append(StringUtils.getString(string.rewards))
            .append(":  ")
            .setForegroundColor(ColorUtils.getColor(color.color_202028))
            .append("$sendGainCoin")
            .setForegroundColor(ColorUtils.getColor(color.color_FE4F3C))
            .create()
        mBinding.tvRewards.text = spanRewards
        mBinding.tvAllBeans.text = "${gainCoin + sendGainCoin} "
    }

    private var clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {

        }

        override fun updateDrawState(paint: TextPaint) {
            paint.color = ColorUtils.getColor(R.color.color_1a1a1a)
            paint.bgColor = Color.TRANSPARENT
        }
    }

}